package com.weipai.springboot.module.controller;

import com.weipai.springboot.model.*;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.constraints.NotBlank;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/22 15:24
 * <p>
 * 商品控制类
 */
@RestController
@Validated
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    FileUploadService fileUploadService;
    

    /**
     * 添加货品与商品
     * @param product
     * @return
     */
    @PostMapping("/goods/save")
    public Response save(Product product, ProductDesc productDesc, String goodsInfo, String productPro) { 
        return goodsService.save(product, productDesc, goodsInfo, productPro);
    }

    /**
     * 更新货品
     *
     * @param productSpec：规格
     * @param productName：货品名称
     * @param productDesc：货品详情
     * @return
     */
    @PostMapping("/goods/updateProduct")
    public Response updateProduct(String productId, String productSpec, String productName,
                                  ProductDesc productDesc, Integer consumeIntegral, Integer commentIntegral,
                                  Integer isBoutique, Integer isNews, Integer isPackage) {
        return goodsService.updateProduct(productId, productSpec, productName, productDesc, consumeIntegral,
                commentIntegral, isBoutique, isNews, isPackage);
    }

    /**
     * 新增商品
     *
     * @param goodsInfo：商品的信息
     * @param spuNumber：货品编号
     * @return
     */
    @PostMapping("/goods/saveGoods")
    public Response saveGoods(String spuNumber, String goodsInfo) {
        return goodsService.saveGoods(spuNumber, goodsInfo);
    }


    /**
     * 根据product的id查询详情
     *
     * @param id：product的id
     * @return
     */
    @GetMapping("/goods/getProductInfoById01/{id}")
    public Response getProductInfoById01(@PathVariable("id") String id) {
        return goodsService.getProductInfoById01(id);
    }

    /**
     * 根据product的id查询详情
     *
     * @param id：product的id
     * @param categoryId：product类别的id
     * @param spuNumber：spuNumber货品编号
     * @return
     */
    @GetMapping("/goods/getProductInfoById02/{id}/{categoryId}/{spuNumber}")
    public Response getProductInfoById02(@PathVariable("id") String id,
                                         @PathVariable("categoryId") String categoryId,
                                         @PathVariable("spuNumber") String spuNumber) {
        return goodsService.getProductInfoById02(id, categoryId, spuNumber);
    }

    /**
     * 秒杀商品和会场商品--前端商品详情
     * @param id
     * @return
     */
    @GetMapping("/goods/getSpecialGoodsInfo/{id}")
    public Response getSpecialGoodsInfo(@PathVariable("id") String id){
        return goodsService.getSpecialGoodsInfo(id);
    }

    /**
     * 通过商品的id获取商品详情
     * @param goodsId
     * @return
     */
    @GetMapping("/goods/getGoodsInfoByGoodsId/{goodsId}")
    public Response getGoodsInfoByGoodsId(@PathVariable("goodsId") String goodsId){
        return goodsService.getGoodsInfoByGoodsId(goodsId);
    }

    /**
     * 删除商品
     *
     * @param id：goods的id
     * @return
     */
    @GetMapping("/goods/deleteByProductById/{id}")
    public Response deleteByProductById(@PathVariable("id") String id) {
        return goodsService.deleteByGoodsById(id);
    }

    /**
     * 删除产品
     * @param spuNumber
     * @return
     */
    @GetMapping("/goods/deleteProductBySpuNumber/{spuNumber}")
    public Response deleteProductBySpuNumber(@PathVariable("spuNumber") String spuNumber){
        return goodsService.deleteProductBySpuNumber(spuNumber);
    }

    /**
     * 修改商品
     *
     * @param goodsId：商品id
     * @param goodsInfo：商品信息
     * @return
     */
    @PostMapping("/goods/updateGoods")
    public Response updateGoods(String goodsId, String goodsInfo) {
        return goodsService.updateGoods(goodsId, goodsInfo);
    }

    /**
     * 更改商品的状态
     *
     * @param id：goods的id
     * @return
     */
    @GetMapping("/goods/changeGoodsStatus/{id}/{status}")
    public Response changeGoodsStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return goodsService.changeGoodsStatus(id, status);
    }

    /**
     * 更改产品的状态
     * @param spuNumber：产品的编号
     * @param status：状态码
     * @return
     */
    @GetMapping("/goods/changeProductStatus/{spuNumber}/{status}")
    public Response changeProductStatus(@PathVariable("spuNumber") String spuNumber, @PathVariable("status") Integer status){
        return goodsService.changeProductStatus(spuNumber, status);
    }

    /**
     * 检索商品
     * @param search
     * @return
     */
    @GetMapping("/goods/searchGoods/{search}/{page}/{size}")
    public Response searchGoods(@PathVariable("search") String search,
                                @PathVariable("page") Integer page,
                                @PathVariable("size") Integer size){
        return goodsService.searchGoods(search, page, size);
    }

    /**
     * 产品审核
     *
     * @param id：goods的id
     * @return
     */
    @GetMapping("/goods/changeIsAudit/{id}/{status}")
    public Response changeIsAudit(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return goodsService.changeIsAudit(id, status);
    }

    /**
     * 根据第三级类别id查询商品
     *
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/goods/searchGoodsByCategoryId")
    public Response searchGoodsByCategoryId(String categoryId, int page, int size, String draw) {
        return goodsService.searchGoodsByCategoryId(categoryId, page, size, draw);
    }

    /**
     * 根据类别查询精品商品
     *
     * @param categoryId
     * @param size
     * @return
     */
    @GetMapping("/goods/searchBoutiqueGoods")
    Response searchBoutiqueGoods(String shopId, String categoryId, Integer size) {
        return goodsService.searchBoutiqueGoods(shopId, categoryId, size);
    }

    //人气精选商品
    @GetMapping("/goods/searchPopularBountiqueGoods")
    Response searchPopularBountiqueGoods(String shopId, Integer size) {
        return goodsService.searchPopularBountiqueGoods(shopId, size);
    }

    //店铺内部-人气精选商品
    @GetMapping("/goods/searchPopularGoodsByShop")
    Response searchPopularGoodsByShop(String shopId, Integer page, Integer size) {
        return goodsService.searchPopularGoodsByShop(shopId, page, size);
    }

    //大牌精选商品(待定)
    @GetMapping("/goods/searchBigBrandBountiqueGoods")
    Response searchBigBrandBountiqueGoods(Integer size) {
        return goodsService.searchBigBrandBountiqueGoods(size);
    }

    //当前浏览商品的相关商品列表
    @GetMapping("/goods/relatedGoods")
    Response searchRelatedGoods(String goodsId, Integer size) {
        return goodsService.searchRelatedGoods(goodsId, size);
    }

    //在一级工具下的相关推荐商品列表
    @GetMapping("/goods/searchRecommendGoods")
    Response searchRecommendGoods(String categoryId, Integer size) {
        return goodsService.searchRecommendGoods(categoryId, size);
    }

    //限时抢购列表
    @GetMapping("/goods/searchSpecialGoods")
    public Response searchSpecialGoods(int page, int size, String draw) {
        return goodsService.searchSpecialGoods(page, size, draw);
    }

    //新品商品列表
    @GetMapping("/goods/searchNewGoods")
    Response searchNewGoods(Integer size) {
        return goodsService.searchNewGoods(size);
    }

    /**
     * 前端商城展示商品详情-----------------------------------------------------------------------------------------------------------
     *
     * @param spuNumber
     * @return
     */
    @GetMapping("/goods/getProductInfoByProductId")
    public Response getProductInfoBySpuNumber(@RequestParam("spuNumber") String spuNumber,
                                              @RequestParam(value = "customerId", required = false) String customerId) {
        return goodsService.getProductInfoBySpuNumber(spuNumber, customerId);
    }

    /**
     *
     * ####################################
     * 以下为商品索引
     * ####################################
     *
     */

    /**
     * 将商品添加到索引库
     * @param id 商品goods_id
     * @return
     */
    @GetMapping("/goods/saveGoodsIndex")
    public Response saveGoodsIndex(String id) {
        return goodsService.saveGoodsIndex(id);
    }
 
    // 一键添加所有商品到索引库
    @GetMapping("/goods/saveGoodsIndex22")
    public Response saveGoodsIndex22() {
        return goodsService.saveGoodsIndex();
    }


    // 一键修改商品名称同步产品名称
    @GetMapping("/goods/updataProductToGoods")
    public Response updataProductToGoods() {
        return goodsService.updataProductToGoods();
    }


   /*  一键删除所有 索引库
     <delete><query>*:*</query></delete>
    <commit/>
         */
//    // 一键删除所有商品到索引库
//    @GetMapping("/goods/deleteGoodsIndex22")
//    public Response deleteGoodsIndex22( ) {
//        return goodsService.deleteGoodsIndex();
//    }

    /**
     * 将商品从索引库删除
     * @param id 商品goods_id
     * @return
     */
    @GetMapping("/goods/deleteGoodsIndex")
    public Response deleteGoodsIndex(String id) {
        return goodsService.deleteGoodsIndex(id);
    }


    /**
     * 关键字搜索入口
     * @param query      关键字
     * @param isNews     是否新品 0 否 1 是
     * @param categoryId 分类id
     * @param brandName  品牌名称
     * @param minPrice   价格区间最低价格
     * @param maxPrice   价格区间最高价格
     * @param prop      属性搜索字符串数组
     * @param page       页号
     * @param size       行数
     * @param isBigBrand       是否大牌
     * @return
     */
    @GetMapping("/goods/searchGoodsByKeyWord")
    public Response searchGoodsByKeyWord(String query,
                                         Integer isNews,
                                         Integer isBoutique,
                                         Integer isBigBrand,
                                         String categoryId,
                                         String brandName,
                                         Integer minPrice,
                                         Integer maxPrice,
                                         String[] prop,
                                         Integer page,
                                         Integer size,
                                         String sort,   //热销关键字
                                         String shopId,
                                         String way,
                                         String clientType,
                                         Integer isHomePage) {
        return goodsService.searchGoodsByKeyWord(query, isNews, isBoutique,isBigBrand, categoryId, brandName,
                minPrice, maxPrice, prop, page, size, sort, shopId, way, clientType,isHomePage);
    }

    /**
     * 从数据库搜索销量数据
     * @return
     */
    @GetMapping("/goods/searchBySoldOutGoods")
    public Response searchBySoldOutGoods(){
        return goodsService.searchBySoldOutGoods();
    }


    //APP 首页 热销商品   上一级分类
    @GetMapping("/goods/searchHotCategory")
    public Response searchGoodsByKeyWord22(
                                         Integer page,          //页数
                                         Integer size,          // 每页 个数
                                         String sort,           //按字段 排序
                                         String way              //正序, 倒序
                                                    ) {
        return goodsService.searchGoodsByKeyWord22( page, size, sort, way);
    }

    /**
     * 通过商品编码搜索商品
     * @param skuNumber
     * @param customerId
     * @return
     */
    @GetMapping("/goods/getGoodsBySkuNumber")
    public Response getGoodsBySkuNumber(@NotBlank(message = "商品编号不能为空") String skuNumber, 
                                        @NotBlank(message = "用户id不能为空") String customerId){
        return goodsService.getGoodsBySkuNumber(skuNumber, customerId);
    }






}
